//
//  ForgetPassController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/8.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ForgetPassController.h"
#import "PersonNavView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "Manager.h"
#import "SetPassController.h"
CG_INLINE void AlertLog (NSString *titleStr,NSString *message,NSString *button1,NSString *button2)
{
    if(!titleStr)
        titleStr = @"";
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: titleStr
                                                        message: message
                                                       delegate: nil
                                              cancelButtonTitle: button1
                                              otherButtonTitles: button2,
                              nil];
    [alertView show];
    
}

@interface ForgetPassController ()
{
    UIView *_texetView;
    UIButton *_taseBnt;
    
    UILabel *_timeLab;
    UITextField *_phoneNub;
    UITextField *_testText;
    
    NSTimer *_timer;

}
@end
static int count;
@implementation ForgetPassController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
      [self NavView];
}
#pragma mark--自定义导航
-(void)NavView{
    
    PersonNavView *naview = [[PersonNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    naview.title.text = @"忘记密码";
    
    naview.BackToFatherView = ^{
        
        if (_phoneNub.text.length==0&&_testText.text.length==0) {
            [self.navigationController popViewControllerAnimated:NO];
        }else{
            
            UIAlertController *alert = [self ShowAlertWhthTitle:@"提示" Message:@"您确定放弃此次的修改" CancelButtonTitle:@"取消"];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:NO];
            }];
            
            [alert addAction:cancel];
        }
    };
    [self.view addSubview:naview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self CrearTextField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keBoyTap)];
    [self.view addGestureRecognizer:tap];
   
}

-(void)keBoyTap{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

-(void)CrearTextField{
    
    _texetView =[self Linview:CGRectMake(-1, 84, SCREEN_WIDTH+2, screen_height*0.2) BackgroundColor:[UIColor whiteColor] AddView:self.view];
    _texetView.backgroundColor =  [UIColor whiteColor];
    _texetView.layer.borderWidth = 1;
    _texetView.layer.borderColor = RGBA(225, 225, 225, 1).CGColor;
    _texetView.layer.masksToBounds = YES;
    
    [self Linview:CGRectMake(13, H(_texetView)/2, SCREEN_WIDTH-30, 1) BackgroundColor:RGBA(225, 225, 225, 1) AddView:_texetView];
    
    
    UILabel *label =[self CreatLabelWhithRect:CGRectMake(10, 4, 35,  H(_texetView)/2-5) Text:@"+86" TextColor:[UIColor blackColor] Font:0.05 AddView:_texetView];
    label.textAlignment = NSTextAlignmentCenter;
    
    /*获取验证码Bnt*/
    _taseBnt = [self CreatBntAddView:_texetView WhithRect:CGRectMake(Width(_texetView)-70-10, 10, 70, H(_texetView)/2-20) AndType:UIButtonTypeCustom AndBntImgName:@"yugv" AndTitle:@"获取验证码" Font:12];
    _taseBnt.adjustsImageWhenDisabled = NO;
    _taseBnt.adjustsImageWhenHighlighted = NO;
    [_taseBnt addTarget:self action:@selector(getVerification) forControlEvents:UIControlEventTouchUpInside];
    [_taseBnt setTitleColor:mainColor forState:UIControlStateNormal];
    _taseBnt.layer.borderWidth = 1;
    _taseBnt.layer.borderColor =mainColor.CGColor;
    radius(_taseBnt, 3);
    
    
    //手机号text
    _phoneNub= [self CrectTextFieldWhithRect:CGRectMake(Width(label), 4, W(_texetView)-W(label)-W(_taseBnt)-25, H(_texetView)/2-5) Placeholder: @"手机号" FatherView:_texetView];
    _phoneNub.keyboardType = UIKeyboardTypeNumberPad;
    
    //验证码输入框
    _testText = [self CrectTextFieldWhithRect:CGRectMake(15, Height(_phoneNub)+4, SCREEN_WIDTH-30,H(_phoneNub)) Placeholder:@"请输入验证码" FatherView:_texetView];
    _testText.keyboardType = UIKeyboardTypeNumberPad;
    
    UIButton *nextBnt = [self CreatBntAddView:self.view WhithRect:CGRectMake(X(_testText),Height(_texetView)+15, SCREEN_WIDTH-X(_testText)*2, 40) AndType:UIButtonTypeSystem AndBntImgName:@"aiksdn," AndTitle:@"下一步" Font:SCREEN_WIDTH*0.07];
    radius(nextBnt, 3);
    nextBnt.backgroundColor = mainColor;
    [nextBnt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBnt addTarget:self action:@selector(nextCtl) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark----发送短信验证码
-(void)getVerification{
    //判断手机号码是否符合规则
    NSString  *rule = @"^0{0,1}(13[0-9]|15[3-9]|15[0-2]|18[0-9]|17[5-8]|145|147|170)[0-9]{8}$";
    NSPredicate* pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule];
    BOOL isMatch = [pred evaluateWithObject:_phoneNub.text];
    if (!isMatch)
    {
        AlertLog(@"提示", @"手机号码不合法请重新填", @"好", nil);
        return;
    }
    
    
    
    [_taseBnt setTitle:@"60S" forState:UIControlStateNormal];
    count = 0;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (_phoneNub.text.length!=0) {
            NSDictionary *dic = @{ @"mobile":_phoneNub.text,
                                   @"formhash":responseObject[@"data"][@"formhash"],
                                   @"type":@"forget_pwd"};
            [self getSMSWhithdic:dic];//发短信验证 请求  要先获取 formhash
        }
        
        
    }];
}
#pragma mark--获取fromhash后 发送短信验证请求
-(void)getSMSWhithdic:(NSDictionary *)dic{
    
    [[Manager sharedManager]ResponseWhithURL:SMSUrl AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            
            
        }else{
            AlertLog(@"发送失败", @"请重新获取验证码", @"好", nil);
            
        }
    }];
}


-(void)updateTime
{
    count++;
    count++;
    if (count >= 61)
    {
        //停止定时器
        [_timer invalidate];
        [_taseBnt setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        return;
    }
    [_taseBnt setTitle:[NSString stringWithFormat:@"%iS",60-count] forState:UIControlStateNormal];

}

#pragma mark--验证手机验证码 并到下一个页面
-(void)nextCtl{
    
    NSDictionary *dic = @{@"mobile":_phoneNub.text,
                          @"sms_code":_testText.text,
                          };
    [[Manager sharedManager]ResponseWhithURL:SMSverify AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if ([responseObject[@"status"]isEqualToString:@"SUCCESS"]) {
            SetPassController *set = [SetPassController new];
            set.mobile = _phoneNub.text;
            set.smsCode = _testText.text;
            [self.navigationController pushViewController:set animated:YES];
        }else{
            
            [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
            
        }
    }];


//    if (_phoneNub.text.length!=11||_testText.text.length==0) {
//        [Manager GetHudWhithText:@"请正确填写信息" AndFatherView:self.view];
//    }else{
//        
//        [self Submit];
//    }
}
-(void)Submit{
    
    }

@end
